package com.example.function.integration.exception;

import com.example.function.integration.response.ResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.example.function.integration.constant.CommonConstant.*;

/**
 * @Description：全局异常处理类
 * @Author： fengcheng
 * @Date： 2024/10/12 09:15
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    public static String errorCode = String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR);

    /**
     * @Description：全局捕获业务异常
     * @Author：fengcheng
     * @Date：2024/10/12 09:45
     * @Param：e
     * @ParamsType：[com.example.function.integration.exception.BizException]
     * @Return：com.example.function.integration.response.ResponseBody
     */
    @ExceptionHandler(value = BizException.class)
    public ResponseBody handleBizException(BizException e) {
        log.error(GLOBAL_EXCEPTION_MSG + MID_BAR + BUSINESS_EXCEPTION_MSG + MID_BAR + e.getMessage(), e);
        return ResponseBody.error(errorCode, BUSINESS_ERROR_MSG + MID_BAR + e.getMessage());
    }

    /**
     * @Description：全局捕获运行时异常
     * @Author：fengcheng
     * @Date：2024/10/12 09:45
     * @Param：e
     * @ParamsType：[java.lang.RuntimeException]
     * @Return：com.example.function.integration.response.ResponseBody
     */
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseBody handleRuntimeException(RuntimeException e) {
        log.error(GLOBAL_EXCEPTION_MSG + MID_BAR + RUNTIME_EXCEPTION_MSG + MID_BAR + e.getMessage(), e);
        return ResponseBody.error(errorCode, SYSTEM_ERROR_MSG);
    }

    /**
     * @Description：全局捕获空指针异常
     * @Author：fengcheng
     * @Date：2024/10/12 09:45
     * @Param：e
     * @ParamsType：[java.lang.NullPointerException]
     * @Return：com.example.function.integration.response.ResponseBody
     */
    @ExceptionHandler(value = NullPointerException.class)
    public ResponseBody handleNullPointerException(NullPointerException e) {
        log.error(GLOBAL_EXCEPTION_MSG + MID_BAR + NULL_EXCEPTION_MSG + MID_BAR + e.getMessage(), e);
        return ResponseBody.error(errorCode, CODE_ERROR_MSG + MID_BAR + e.getMessage());
    }

    /**
     * @Description：处理实体类参数注解的异常
     * @Author：fengcheng
     * @Date：2024/10/12 10:04
     * @Param：e
     * @ParamsType：[org.springframework.web.bind.MethodArgumentNotValidException]
     * @Return：com.example.function.integration.response.ResponseBody
     */
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ResponseBody handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error(GLOBAL_EXCEPTION_MSG + MID_BAR + PARAM_EXCEPTION_MSG + COLON + e.getMessage(), e);
        return ResponseBody.error(errorCode, CHECK_ERROR_MSG + COLON + PARAM_EXCEPTION_MSG);
    }

    /**
     * @Description：全局捕获其他异常
     * @Author：fengcheng
     * @Date：2024/10/12 09:45
     * @Param：e
     * @ParamsType：[java.lang.Exception]
     * @Return：com.example.function.integration.response.ResponseBody
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseBody handleException(Exception e) {
        log.error(GLOBAL_EXCEPTION_MSG + e.getMessage(), e);
        return ResponseBody.error(errorCode, SYSTEM_ERROR_MSG);
    }
}
